/// 5 most common indications and diagnosis Emergency ///
tab indication if muestra==1 & ref_type==1

g emergency_indication=.
replace emergency_indication=1 if indication=="sangrado por vagina" | indication=="hemorragia posparto" & muestra==1 & ref_type==1
replace emergency_indication=2 if indication=="parto prolongado" & muestra==1 & ref_type==1
replace emergency_indication=3 if indication=="presion arterial elevada" | indication=="cefalea" | indication=="perdida del conocimiento" | indication=="historia de presion alta " | indication=="convulsiones" | indication=="mareos" & muestra==1 & ref_type==1
replace emergency_indication=4 if indication=="fiebre" | indication=="taquicardia fetal" | indication=="Secrecion vaginal" & muestra==1 & ref_type==1
replace emergency_indication=5 if indication=="dolor abdominal" | indication=="dolor en cintura" & muestra==1 & ref_type==1
replace emergency_indication=6 if indication=="ruptura espontanea de membranas fetales " & muestra==1 & ref_type==1
replace emergency_indication=7 if indication=="presentacion fetal anormal" & muestra==1 & ref_type==1

/// Diagnosticos finales indicaciones de emergencia ///
///1. Sangrado vaginal ///
tab diagnosis if emergency_indication==1
tab num_open if emergency_indication==1 & muestra==1 & ref_type==1

///2. Hypertensive symptoms or high blood pressure by midwife ///
tab diagnosis if emergency_indication==3 
tab num_open if emergency_indication==3 & muestra==1 & ref_type==1

///3. Premature rupture of membranes ///
tab diagnosis if emergency_indication==6
tab num_open if emergency_indication==6 & muestra==1 & ref_type==1

///4. Prolonged labor ///
tab diagnosis if emergency_indication==2
tab num_open if emergency_indication==2 & muestra==1 & ref_type==1

///5. Abdominal pain ///
tab diagnosis if emergency_indication==5
tab num_open if emergency_indication==5 & muestra==1 & ref_type==1

//// Measuring accuracy of indications vs final diagnosis emergency referrals ////
tab diagnosis if emergency_indication==1 & muestra==1 & ref_type==1
tab diagnosis if emergency_indication==3 & muestra==1 & ref_type==1
tab diagnosis if emergency_indication==6 & muestra==1 & ref_type==1
tab diagnosis if emergency_indication==2 & muestra==1 & ref_type==1
tab diagnosis if emergency_indication==5 & muestra==1 & ref_type==1


////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

///Diagnosticos finales indicaciones de rutina ////
g routine_indication=.
replace routine_indication=1 if muestra==1 & ref_type==2 & indication=="presion arterial elevada" | indication=="cefalea" | indication=="problemas con la vista" | indication=="historia de presion alta " | indication=="historia de presion alta" | indication=="Dificultad para respirar" | indication=="mareos" 
replace routine_indication=2 if muestra==1 & ref_type==2 & indication=="depresion mayor" 
replace routine_indication=3 if muestra==1 & ref_type==2 & indication=="dolor abdominal" | indication=="dolor en cintura" 
replace routine_indication=4 if muestra==1 & ref_type==2 & indication=="infeccion urinaria" | indication=="prurito vaginal" | indication=="fiebre" | indication=="infeccion en herida operatoria"| indication=="infeccion herida operatoria" | indication=="Secrecion vaginal"
replace routine_indication=5 if muestra==1 & ref_type==2 & indication=="verrugas genitales" 
replace routine_indication=6 if muestra==1 & ref_type==2 & indication=="antecedente de obito fetal" 
replace routine_indication=7 if muestra==1 & ref_type==2 & indication=="antecedente de cesarea previa" 

///1. Signs of preeclampsia and history of high blood pressure ///
tab num_open if routine_indication==1 & muestra==1 & ref_type==2

///2. Depression ///
tab num_open if routine_indication==2 & muestra==1 & ref_type==2

///3. Abdominal pain ///
tab num_open if routine_indication==3 & muestra==1 & ref_type==2

///4. Signs of infection (fever, UTI, vaginal itching) ///
tab num_open if routine_indication==4 & muestra==1 & ref_type==2

///5. Genital warts ///
tab num_open if routine_indication==5 & muestra==1 & ref_type==2

///6. History of stillbirth ///
tab num_open if routine_indication==6 & muestra==1 & ref_type==2

///7. History of previous C-section ///
tab num_open if routine_indication==7 & muestra==1 & ref_type==2


//// Measuring accuracy of indications vs final diagnosis routine referrals ////
tab diagnosis if routine_indication==1 & muestra==1 & ref_type==2
tab diagnosis if routine_indication==2 & muestra==1 & ref_type==2
tab diagnosis if routine_indication==3 & muestra==1 & ref_type==2
tab diagnosis if routine_indication==4 & muestra==1 & ref_type==2
tab diagnosis if routine_indication==5 & muestra==1 & ref_type==2
tab diagnosis if routine_indication==6 & muestra==1 & ref_type==2
tab diagnosis if routine_indication==7 & muestra==1 & ref_type==2
